package com.haople.sso.core.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

public class AES {
	private static final Logger logger=Logger.getLogger(AES.class);

	
	/**
	 * 获取密钥
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	private static SecretKey getSecretKey(String secretCode) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		//创建密码生产器
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128, new SecureRandom(secretCode.getBytes("utf-8")));
		SecretKey secretKey=kgen.generateKey();
        SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), "AES");// 转换为AES专用密钥
        return key;
	}
	
	/**
	 * 加密
	 * @param str
	 * @return
	 */
	public static byte[] encrypt(String str,String secretCode){
		try {
			//创建密码计算器
			Cipher cipher=Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(secretCode)); //加密模式
			byte[] bytes=cipher.doFinal(str.getBytes("utf-8"));
			return bytes;
		} catch (NoSuchAlgorithmException e) {
			logger.error("---AES--encrypt--加密异常---", e);
		} catch (NoSuchPaddingException e) {
			logger.error("---AES--encrypt--加密异常---", e);
		} catch (InvalidKeyException e) {
			logger.error("---AES--encrypt--加密异常---", e);
		} catch (UnsupportedEncodingException e) {
			logger.error("---AES--encrypt--加密异常---", e);
		} catch (IllegalBlockSizeException e) {
			logger.error("---AES--encrypt--加密异常---", e);
		} catch (BadPaddingException e) {
			logger.error("---AES--encrypt--加密异常---", e);
		}
		return null;
	}
	
	/**
	 * 解密
	 * @param encryptCode
	 * @return
	 */
	public static String decrypt(byte[] encryptBytes,String secretCode){
		try {
			Cipher cipher=Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, getSecretKey(secretCode)); //解密模式
			byte[] bytes=cipher.doFinal(encryptBytes);
			return new String(bytes, "utf-8");
		} catch (InvalidKeyException e) {
			logger.error("---AES--decrypt--解密异常---", e);
		} catch (NoSuchAlgorithmException e) {
			logger.error("---AES--decrypt--解密异常---", e);
		} catch (NoSuchPaddingException e) {
			logger.error("---AES--decrypt--解密异常---", e);
		} catch(UnsupportedEncodingException e) {
			logger.error("---AES--decrypt--解密异常---", e);
		} catch (IllegalBlockSizeException e) {
			logger.error("---AES--decrypt--解密异常---", e);
		} catch (BadPaddingException e) {
			logger.error("---AES--decrypt--解密异常---", e);
		}
		return null;
	}
	
}
